from django.http import JsonResponse
from django.shortcuts import render, HttpResponse, redirect
from app01 import models


# Create your views here.

def index(request):
    return render(request, 'index.html')


def ajax_calculator(request):
    if request.method == 'POST':
        print(request.POST)
        a = request.POST.get('a')
        b = request.POST.get('b')
        sum_num = int(a) + int(b)
        return JsonResponse({'sum_num': sum_num})
    return render(request, 'ajax_calculator.html')


def book_list(request):
    book_queryset = models.Book.objects.all()
    return render(request, 'book_list.html', context=locals())


def book_add(request):
    # 获取所有出版社和所有作者
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()

    if request.method == 'POST':
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_date = request.POST.get('publish_date')
        publish_id = request.POST.get('publish_id')
        authors = request.POST.getlist('authors')

        book_obj = models.Book.objects.create(title=title, price=float(price), publish_date=publish_date,
                                              publish_id=publish_id)
        book_obj.authors.add(*authors)

        return redirect("/book/list/")

    return render(request, 'book_add.html', context=locals())


def book_modify(request):
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    book_id = request.GET.get('book_id')
    current_book = models.Book.objects.filter(pk=book_id).first()

    if request.method == 'POST':
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_date = request.POST.get('publish_date')
        publish_id = request.POST.get('publish_id')
        authors = request.POST.getlist('authors')

        models.Book.objects.filter(pk=book_id).update(title=title, price=float(price),
                                                      publish_date=publish_date,
                                                      publish_id=publish_id)
        current_book.authors.set(authors)
        return redirect("/book/list/")

    return render(request, 'book_modify.html', context=locals())


def book_delete(request):
    book_id = request.GET.get('book_id')
    models.Book.objects.filter(pk=book_id).first().delete()
    return redirect("/book/list/")


def author_list(request):
    author_queryset = models.Author.objects.all()
    return render(request, 'author_list.html', context=locals())


def author_add(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        gender = request.POST.get('gender')
        age = request.POST.get('age')
        phone = request.POST.get('phone')
        address = request.POST.get('address')

        author_detail_obj = models.AuthorDetail.objects.create(age=age, phone=phone, address=address)
        author_obj = models.Author.objects.create(name=name, gender=gender, author_detail=author_detail_obj)
        return redirect("/author/list/")

    return render(request, 'author_add.html', context=locals())


def author_modify(request):
    author_id = request.GET.get('author_id')
    current_author = models.Author.objects.filter(pk=author_id).first()

    if request.method == 'POST':
        name = request.POST.get('name')
        gender = request.POST.get('gender')
        age = request.POST.get('age')
        phone = request.POST.get('phone')
        address = request.POST.get('address')

        models.Author.objects.filter(pk=author_id).update(name=name, gender=gender)
        models.AuthorDetail.objects.filter(pk=current_author.author_detail_id).update(age=age, phone=phone,
                                                                                      address=address)
        return redirect("/author/list/")

    return render(request, 'author_modify.html', context=locals())


def author_delete(request):
    author_id = request.GET.get('author_id')
    models.Author.objects.filter(pk=author_id).first().delete()
    return redirect("/author/list/")


def publish_list(request):
    publish_queryset = models.Publish.objects.all()
    return render(request, 'publish_list.html', context=locals())


def publish_add(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        addr = request.POST.get('addr')

        publish_obj = models.Publish.objects.create(name=name, addr=addr)
        return redirect("/publish/list/")

    return render(request, 'publish_add.html', context=locals())


def publish_modify(request):
    publish_id = request.GET.get('publish_id')
    current_publish = models.Publish.objects.filter(pk=publish_id).first()

    if request.method == 'POST':
        name = request.POST.get('name')
        addr = request.POST.get('addr')

        models.Publish.objects.filter(pk=publish_id).update(name=name, addr=addr)
        return redirect("/publish/list/")

    return render(request, 'publish_modify.html', context=locals())


def publish_delete(request):
    publish_id = request.GET.get('publish_id')
    models.Publish.objects.filter(pk=publish_id).first().delete()
    return redirect("/publish/list/")
